package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.mapper.OrderMapper;
import com.itheima.reggie.service.AddressBookService;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrderService;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper mapper;

    @Autowired
    private AddressBookService addressBookService;

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 提交订单
     *
     * @param orders
     * @param user
     * @return
     */
    @Override
    public int add(Orders orders, User user) {
        //1. 往订单表里面添加数据
        //1.1 先补充orders的数据
        orders.setId(IdWorker.getId());
        //1.1.1 设置number， 数量
        orders.setNumber(orders.getId() + "");
        //1.1.2 设置订单的状态
        orders.setStatus(1);
        //1.1.3 设置用户id
        orders.setUserId(user.getId());
        //1.1.5 设置提交订单时间
        orders.setOrderTime(LocalDateTime.now());
        //1.1.6 设置付款时间
        orders.setCheckoutTime(LocalDateTime.now());
        //1.1.10 提交订单的用户用户名
        orders.setUserName(user.getName());

        AddressBook addressBook = addressBookService.getById(orders.getAddressBookId());
        //1.1.11 收货人的电话
        orders.setPhone(addressBook.getPhone());
        //1.1.12 收货人的地址
        orders.setAddress(
                (addressBook.getProvinceName() != null ? addressBook.getProvinceName() : "") +
                (addressBook.getCityName() != null ? addressBook.getCityName() : "") +
                (addressBook.getDistrictName() != null ? addressBook.getDistrictName() : "") +
                (addressBook.getDetail() != null ? addressBook.getDetail() : "")
        );
        //1.1.13 收货人的名字
        orders.setConsignee(addressBook.getConsignee());
        //1.2 往订单表添加订单
        int rows = mapper.insert(orders);

        //2. 往订单详情表添加数据
        //2.1 先从购物车里面拿出来数据
        List<ShoppingCart> cartList = shoppingCartService.list();

        int rows2 = 0;

        //2.2 遍历购物车的数据，每一条购物车数据就是一个订单详情对象
        for (ShoppingCart cart : cartList) {
            //2.3 遍历一次购物车对象，就创建一个订单详情对象
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setId(IdWorker.getId());
            //2.4 把购物车条目对象拷贝到订单详情对象身上
            BeanUtils.copyProperties(cart, orderDetail);
            //2.5 设置属于哪个订单
            orderDetail.setOrderId(orders.getId());
            //2.6 最后把这个订单详情对象，添加到订单详情表即可
            rows2 += orderDetailService.add(orderDetail);
        }
        //3. 如果订单都已经添加，就清空购物车
        if (rows > 0 && rows2 == cartList.size()) {
            shoppingCartService.clean();
        }
        return rows > 0 && rows2 == cartList.size() ? 1 : 0;
    }
}
